package br.com.catalogodigital.dao;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.http.HttpSession;

import org.primefaces.model.UploadedFile;

import br.com.catalogodigital.entity.Anunciante;
import br.com.catalogodigital.entity.Anuncio;
import br.com.catalogodigital.factory.Factory_Generic;

public class AnuncioDao {

	EntityManager manager;

	public AnuncioDao() {
		Factory_Generic factory = new Factory_Generic();
		manager = factory.manager();
	}

	// / METODOS DAO G ENERICOS
	public void inserir(Object object) {
	
		manager.persist(object);
		manager.getTransaction().commit();
		
	}

	// ALTERAR ANUNCIO
	public void alterar(Anuncio anuncio) {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpSession session = (HttpSession) ec.getSession(false);

		anuncio.getAnunciante().setId(
				(Integer) session.getAttribute("idAnunciante"));


		manager.getTransaction().begin();
		manager.merge(anuncio);
		manager.getTransaction().commit();
	}

	// DELETAR ANUNCIO
	public void deletar(int l) {
		manager.getTransaction().begin();
		Anuncio resultado = manager.find(Anuncio.class, l);
		manager.remove(resultado);
		manager.getTransaction().commit();
		manager.close();
	}

	// BUSCAR ANUNCIO POR ID
	public Anuncio buscarId(int id) {
		manager.getTransaction().begin();
		Anuncio resultado = manager.find(Anuncio.class, id);
		return resultado;
	}

	// INSERIR IMAGEM
	public String iserirImagem(UploadedFile imagem) throws IOException {

		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpSession session = (HttpSession) ec.getSession(false);

		BufferedInputStream in = new BufferedInputStream(
				imagem.getInputstream());

		String contextPath = session.getServletContext().getRealPath("/");
		String fp = contextPath + "Anunciantes" + File.separatorChar
				+ session.getAttribute("idAnunciante") + File.separatorChar
				+ "Anuncio";

		FileOutputStream fout = new FileOutputStream(fp + File.separatorChar
				+ session.getAttribute("idAnuncio") + "_"
				+ imagem.getFileName());

		while (in.available() != 0) {
			fout.write(in.read());
		}
		fout.close();
		
		
		return session.getAttribute("idAnuncio") + "_"
				+ imagem.getFileName();
		
	}

	// BUSCAR ANUNCIO POR EMAIL
	public String buscarEmail(String email) {
		manager.getTransaction().begin();
		Query consultaEmail = manager
				.createQuery("SELECT n FROM Anuncio n WHERE n.email =email");
		List<Anuncio> resultado = consultaEmail.getResultList();
		manager.getTransaction().commit();
		manager.close();
		return resultado.toString();
	}

	// BUSCAR ANUNCIO PELO NOME
	public String buscarNome(String nome) {
		manager.getTransaction().begin();
		Query consultaEmail = manager
				.createQuery("SELECT e FROM Anuncio e WHERE e.nome =nome");
		List<Anuncio> resultado = consultaEmail.getResultList();
		manager.getTransaction().commit();
		manager.close();
		return resultado.toString();
	}

	// LISTA  ANUNCIO POR ANUNCIANTES
	public List<Anuncio> listar() {

		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpSession session = (HttpSession) ec.getSession(false);

		int id = (Integer) session.getAttribute("idAnunciante");

		manager.getTransaction().begin();
		Query consulta = manager
				.createQuery("SELECT x from Anuncio as x WHERE x.anunciante.id ="
						+ id);
		return consulta.getResultList();
	}
	
	public List<Anuncio> listarTodos() {

		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpSession session = (HttpSession) ec.getSession(false);

		manager.getTransaction().begin();
		Query consulta = manager
				.createQuery("select anuncio from Anuncio anuncio");
		manager.close();
		return consulta.getResultList();
	}

	public EntityManager getManager() {
		return manager;
	}

	public void setManager(EntityManager manager) {
		this.manager = manager;
	}

}
